package com.pxxy.yuyi.controller;

import com.github.pagehelper.PageInfo;
import com.pxxy.yuyi.entity.Admin;
import com.pxxy.yuyi.entity.Teachers;
import com.pxxy.yuyi.service.TeachersService;
import com.pxxy.yuyi.utils.ConstUtils;
import com.pxxy.yuyi.utils.ImgeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 *
 * @Author : XuWen
 * @create 2021/6/19 16:55
 */
@Controller
@RequestMapping("/teachers")
public class TeachersController {
    @Autowired
    private TeachersService teachersService;
    //前端展示教师信息
    @RequestMapping("/show")
    public String show(Teachers teachers,HttpServletRequest request,Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }
       PageInfo<Teachers> pageInfo = teachersService.selectTealist(teachers,pageNum);
        request.setAttribute("pi",pageInfo);
        return "teacher";
    }
    //前端展示教师详情
   @RequestMapping("/info")
    public String info (Integer tid, HttpServletRequest request){
      Teachers teachers= teachersService.selectById(tid);
        request.setAttribute("info",teachers);
        return "teacherInfo";
   }
   //后台修改教师信息
   @RequestMapping("/updete")
   //注意图片传输的name不能与Teachers中图片变量名相同，Teachers进行接收封装时，会因类型不同而报错
    public String updete(@RequestParam("imge") MultipartFile imge,Teachers teachers, HttpServletRequest request) throws IOException {
        String timge = ImgeUtils.imgeLoad(imge,request,"upload");
        teachers.setTimge(timge);
       Integer res= teachersService.updateTeacher(teachers);
        return "forward:/teachers/select";
   }
   //后台显示教师信息
   @RequestMapping("/select")
    public String select(HttpServletRequest request,Integer pageNum){
        Teachers teachers = new Teachers();
        if(pageNum==null){
            pageNum=1;
        }
       Admin admin = (Admin) request.getSession().getAttribute(ConstUtils.LOGIN_USER);
       if (admin==null) {//为空则先进行登录
           request.setAttribute("msg", "请先登录");
           return "admin/adminLogin";
       }
       PageInfo<Teachers> pageInfo = teachersService.selectTealist(teachers,pageNum);
       request.setAttribute("pi",pageInfo);
        return "admin/teachers";
   }
   //后台删除教师信息
   @RequestMapping("/delete")
    public String delete(Integer tid){
        teachersService.deleteTeachers(tid);
       return "forward:/teachers/select";
   }
   //后台添加教师信息
   @RequestMapping("/insert")
    public String insert(@RequestParam("imge") MultipartFile imge,Teachers teachers,HttpServletRequest request) throws IOException {
       String timge = ImgeUtils.imgeLoad(imge,request,"upload");
       teachers.setTimge(timge);
        teachersService.insertTeachers(teachers);
       return "forward:/teachers/select";
   }
}
